package com.layton;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer_HelloWorld {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2.设置连接参数
        factory.setHost("127.0.0.1");//默认值为localhost
        factory.setPort(5672);//默认值为5672
        factory.setVirtualHost("/layton");//虚拟机默认为/
        factory.setUsername("layton");//guest
        factory.setPassword("layton");//guest
        //3.获取对应连接connection
        Connection connection = factory.newConnection();
        //4.创建channel
        Channel channel = connection.createChannel();
        //5.创建队列queue,名字有则创建,没有则不会创建
        channel.queueDeclare("hello_world", true, false, false, null);
        //6.发消息
        String body = "hello world2";
        channel.basicPublish("","hello_world",null,body.getBytes());
        //7.释放资源
        channel.close();
        connection.close();
    }
}
/*
    String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
    参数:
        queue 队列名称
        durable 是否持久化,当mq重启之后还在
        exclusive 是否独占:只能有一个消费者监听队列 当connection关闭时是否删除队列
        autoDelete 是否自动删除:当没有consumer会自动删除自己
        arguments 不讲
 */

/*
    String exchange, String routingKey, BasicProperties props, byte[] body
    参数:
        exchange 交换机的名称 简单模式下交换机会使用默认的
        routingKey 路由名称
        props 配置信息
        body 字节数组 真实的消息数据
 */